package com.file_.transformation;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

/*
 * 使用转换流OutputStreamWriter
 * 将FileOutputStream字节流转换成字符流OutputStreamWriter，指定编码为utf-8，对文件进行写入
 * */
public class Demo3 {
    public static void main(String[] args) throws IOException {
        String filePath = "D:\\A_Test\\a2.txt";
        String charSet = "utf-8";  // 保存的文件格式
        // 创建OutputStreamWriter对象
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(filePath), "utf-8");
        // 写入字符串
        outputStreamWriter.write("hello你好");
        // 关闭外层流
        outputStreamWriter.close();
        System.out.println("按照编码" + charSet + "保存文件成功");
    }
}
